package artist.graphic.cn.UI;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.HashMap;

import artist.graphic.cn.R;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText username, password;
    private Button bt_username_clear;
    private Button bt_pwd_clear;
    private Button forgive_pwd;
    private Button bt_pwd_eye;
    private Button login;
    private Button register;

    private ImageView iv_qq, iv_weixin, iv_weibo;
    private boolean isOpen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        username = (EditText) findViewById(R.id.username);
// 监听文本框内容变化

        username.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
// 获得文本框中的用户
                String user = username.getText().toString().trim();
                if ("".equals(user)) {
// 用户名为空,设置按钮不可见
                    bt_username_clear.setVisibility(View.INVISIBLE);
                } else {
// 用户名不为空，设置按钮可见
                    bt_username_clear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        password = (EditText) findViewById(R.id.password);
// 监听文本框内容变化
        if (password != null)
            password.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                                          int count) {
// 获得文本框中的用户
                    String pwd = password.getText().toString().trim();
                    if ("".equals(pwd)) {
// 用户名为空,设置按钮不可见
                        bt_pwd_clear.setVisibility(View.INVISIBLE);
                    } else {
// 用户名不为空，设置按钮可见
                        bt_pwd_clear.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
        bt_username_clear = (Button) findViewById(R.id.bt_username_clear);
        bt_username_clear.setOnClickListener(this);
        bt_pwd_clear = (Button) findViewById(R.id.bt_pwd_clear);
        bt_pwd_clear.setOnClickListener(this);

        bt_pwd_eye = (Button) findViewById(R.id.bt_pwd_eye);
        bt_pwd_eye.setOnClickListener(this);
        login = (Button) findViewById(R.id.login);
        login.setOnClickListener(this);
        register = (Button) findViewById(R.id.register);
        register.setOnClickListener(this);
        forgive_pwd = (Button) findViewById(R.id.forgive_pwd);
        forgive_pwd.setOnClickListener(this);

        iv_qq = (ImageView) findViewById(R.id.iv_qq);
        iv_qq.setOnClickListener(this);
        iv_weixin = (ImageView) findViewById(R.id.iv_weixin);
        iv_weixin.setOnClickListener(this);
        iv_weibo = (ImageView) findViewById(R.id.iv_weibo);
        iv_weibo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_username_clear:
// 清除登录名
                username.setText("");
                break;
            case R.id.bt_pwd_clear:
// 清除密码
                password.setText("");
                break;
            case R.id.bt_pwd_eye:
// 密码可见与不可见的切换
                if (isOpen) {
                    isOpen = false;
                } else {
                    isOpen = true;
                }
// 默认isOpen是false,密码不可见
                changePwdOpenOrClose(isOpen);
                break;
            case R.id.login:
// TODO 登录按钮
                break;
            case R.id.register:
// 注册按钮
                SMSVerification();
                break;
            case R.id.forgive_pwd:
// 忘记密码按钮
                Toast.makeText(this, "忘记密码", Toast.LENGTH_SHORT).show();
                break;

            case R.id.iv_qq:
                Toast.makeText(this, "QQ登录暂未实现，请尝试微信登录吧", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_weixin:
                Toast.makeText(this, "微信登录暂未实现，就是坑你点这个的，哈哈~", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_weibo:
                Toast.makeText(this, "微博登录暂未实现，请尝试微信登录吧", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    /**
     * 密码可见与不可见的切换
     *
     * @param flag
     */
    private void changePwdOpenOrClose(boolean flag) {
// 第一次过来是false，密码不可见
        if (flag) {
// 密码可见
            bt_pwd_eye.setBackgroundResource(R.drawable.login_eye_open);
// 设置EditText的密码可见
            password.setTransformationMethod(HideReturnsTransformationMethod
                    .getInstance());
        } else {
// 密码不接见
            bt_pwd_eye.setBackgroundResource(R.drawable.login_eye_close);
// 设置EditText的密码隐藏
            password.setTransformationMethod(PasswordTransformationMethod
                    .getInstance());
        }
    }


    public void SMSVerification() {

        SMSSDK.initSDK(this, "2357e0dc033d0", "d2de43b564a002f1a8f202ee83afac59");
        //创建注册页面
        RegisterPage registerPage = new RegisterPage();
        //显示界面
        registerPage.show(this);

        //回调函数
        registerPage.setRegisterCallback(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                // 解析注册结果
                if (result == SMSSDK.RESULT_COMPLETE) {
                    @SuppressWarnings("unchecked")
                    HashMap<String, Object> phoneMap = (HashMap<String, Object>) data;
                    String country = (String) phoneMap.get("country");
                    String phone = (String) phoneMap.get("phone");
                    Toast.makeText(getApplicationContext(), "验证成功", Toast.LENGTH_SHORT).show();
                    // 提交用户信息（此方法可以不调用）
                    //registerUser(country, phone);
                }
            }
        });
    }


}